﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class Pages_Carrier_BusEdit : System.Web.UI.Page
{
	Int64 Bus_id { get { return DS.ToInt64(Request.QueryString["Id"]); } }

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			if ((Session["BusSaved"] ?? string.Empty).ToString() == "SavedGood")
			{
				lbMessage.ForeColor = Color.Green;
				lbMessage.Text = "Данные сохранены успешно";
				Session["BusSaved"] = null;
			}
			if (Bus_id > 0)
				InitUpdateContext();
		}
	}

	private void InitUpdateContext()
	{
		Bus b = DBEntity<Bus>.Load(Bus_id);
		if (b == null)
			Response.Redirect("~/");
		tbBusName.Text = b.Name;
		tbBusPlaceCount.Text = b.PlaceCount.ToString();
	}

	protected void OnBusSave(object s, EventArgs e)
	{
		if (IsValid)
		{
			Bus b = DBEntity<Bus>.Load(Bus_id) ?? new Bus();
			b.Name = tbBusName.Text;
			b.PlaceCount = DS.ToInt(tbBusPlaceCount.Text);
			if (b.Merge())
			{
				if (Bus_id == 0)
				{
					Session["BusSaved"] = "SavedGood";
					Response.Redirect("~/Pages/Carrier/BusEdit.aspx?Id=" + b.Id.ToString());
				}
				else
				{
					lbMessage.ForeColor = Color.Green;
					lbMessage.Text = "Данные сохранены успешно";
				}
			}
			else
			{
				lbMessage.ForeColor = Color.Red;
				lbMessage.Text = "При сохранении данных проихошла ошибка";
			}
		}
	}

	protected void OnCancel(object s, EventArgs e)
	{
		Response.Redirect("~/Pages/Carrier/Bus.aspx");
	}
}
